package com.example.tests;

import static com.example.tests.ContactDataGenerator.generateRandomContact;
import static com.example.tests.GroupDataGenerator.generateRandomGroups;

import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;

import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;

import com.example.fw.ApplicationManager;

public class TestBase {

	protected static ApplicationManager app;
	private int checkFrequency;
	private int checkCounter;


	@BeforeTest
	public void setUp() throws Exception {
		String configfile = System.getProperty("configfile", "application.properties");
		Properties properties = new Properties();
		properties.load(new FileReader(new File(configfile)));
		app = new ApplicationManager(properties);
        checkCounter = 0;
        checkFrequency = Integer.parseInt(properties.getProperty("check.frequency", "0"));
	  }
	
	@AfterTest
	public void tearDown() throws Exception {
		app.stop();
	}
	
	@DataProvider
	public Iterator<Object[]> randomValidGroupgenerator(){
		return wrapGroupDataForProvider(generateRandomGroups(5)).iterator();
		}
	
	
	@DataProvider
	public Iterator<Object[]> randomValidContactGenerator(){
		return wrapContactDataForProvider(generateRandomContact(5)).iterator();
		}
	
	protected boolean wantToCheck(){
	  checkCounter++;		
	  if(checkCounter > checkFrequency){
		  checkCounter = 0; 
		  return true;  
	  } else{
		  return false;
	  }	  
	}
	
	
	protected static List<Object[]> wrapGroupDataForProvider(List<GroupData> groups) {
		List<Object[]> list = new ArrayList<Object[]>();
		for (GroupData group : groups){
			list.add(new Object[]{group});
		}
		return list;
	}
		
	protected static List<Object[]> wrapContactDataForProvider(List<ContactData> contacts){
		List<Object[]> list = new ArrayList<Object[]>();
		for (ContactData contact : contacts){
			list.add(new Object[]{contact});
		}
		return list;
	}
	

}
